#! /usr/bin/env python3
# coding = uft-8

# 6 ZigZag Conversion

class Solution(object):
    def convert(self, s, numRows):
        if numRows == 1:
            return s
        if numRows == 2:
            return str(s[0::2]) + str(s[1::2])

        offset = (numRows -1) * 2
        start_pos = [0, offset]
        res = ""
        for row in range(1, numRows + 1):
            if start_pos[1] - start_pos[0] in [offset, 0]:
                res += s[start_pos[0]:: offset]
            else:
                str1 = s[start_pos[0]:: offset]
                str2 = s[start_pos[1]:: offset]
                for i, j in zip(str1, str2):
                    res += i
                    res += j
                if len(str1) != len(str2):
                    rest = str1[-1] if len(str1) > len(str2) else str2[-1]
                    res += rest

            start_pos[0] += 1
            start_pos[1] -= 1
        return res

s = Solution()
print(s.convert("PAYPALISHIRING", 3))
